package com.sinoglobal.sinostore.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lidroid.xutils.BitmapUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.bean.ImageVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.BitmapHelp;
import com.sinoglobal.sinostore.widget.TouchImageView;

/**
 * 
* @title: TurntableImageActivity.java 
* @package com.sinoglobal.lntv.activity.shop 
* @description: TODO(点击看大图) 
* @author LK
* @date 2014-12-2 下午6:29:57
 */
public class TurntableImageActivity extends Activity {
	private RelativeLayout prize_ad_rel;
	private ViewPager viewPager;
	private ArrayList<ImageVo> bigImages;
	private List<View> mListViews;
	private MyPagerAdapter adapter;
	/**图片指示器**/
	private LinearLayout imgPoint;
	private int index;//点击index
	/**选中的下标**/
	private int preSelImgIndex;
	
	private BitmapUtils bitmapUtils;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.shop_layout_bigmapmodel);
		prize_ad_rel = (RelativeLayout)findViewById(R.id.prize_ad_rel);
		imgPoint=(LinearLayout)findViewById(R.id.prize_ad_text_rel);
		viewPager = (ViewPager)this.findViewById(R.id.viewpager);
		
		index = getIntent().getExtras().getInt("index");
		bigImages = (ArrayList<ImageVo>)getIntent().getExtras().get("imgurls");
		bitmapUtils = BitmapHelp.getBitmapUtils(this);
		
		initData();
		
		initView();
	}
	
	/**初始化数据**/
	private void initData(){
		mListViews = new ArrayList<View>();
		for(ImageVo image : bigImages){
			TouchImageView imageView = new TouchImageView(getApplicationContext());
			LayoutParams params = new LayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
			imageView.setLayoutParams(params);
			bitmapUtils.display(imageView, Constants.PIC_HOST+image.getUrl());
			mListViews.add(imageView);
		}
		adapter = new MyPagerAdapter(mListViews);
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(index,true);
		// 如果有奖品描述的话，那么显示底部描述，如果没有奖品描述，那么底部栏隐藏
		prize_ad_rel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TurntableImageActivity.this.finish();
			}
		});
	}
	
	/**初始化View**/
	private void initView(){
		preSelImgIndex = index;
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				// 修改上一次选中项的背景
				initPoint(position);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		InitFocusIndicatorContainer(bigImages.size());
		initPoint(index);
	}
	
	/**选中当前项背景**/
	private void initPoint(int position){
		if(null != bigImages && bigImages.size() > 1){
			position = position % bigImages.size();
			ImageView preSelImg = (ImageView)imgPoint.findViewById(preSelImgIndex);
			preSelImg.setImageDrawable(getResources().getDrawable(R.drawable.shop_banner_img_selected));
			// 修改当前选中项的背景
			ImageView curSelImg = (ImageView)imgPoint.findViewById(position);
			curSelImg.setImageDrawable(getResources().getDrawable(R.drawable.shop_banner_img_default));
			preSelImgIndex = position;
		}
	}
	
	/**
	 * 设置轮播图上的小圆点
	 */
	private void InitFocusIndicatorContainer(int bannerSize) {
		if (bannerSize == 1) { // 轮播图如果是一张，则不显示小圆点
			this.imgPoint.setVisibility(View.GONE);
		} else {// 循环添加小圆点
			for (int i = 0; i < bannerSize; i++) {
				ImageView ivBanner = new ImageView(TurntableImageActivity.this);
				ivBanner.setId(i);
				ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
				ivBanner.setScaleType(localScaleType);
				LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				ivBanner.setLayoutParams(localLayoutParams);
				ivBanner.setPadding(5, 5, 10, 5);
				ivBanner.setImageResource(R.drawable.shop_banner_img_selected);
				this.imgPoint.addView(ivBanner);
			}
		}
	}
	
	
	
	/**
     * ViewPager适配器
*/
    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }
        @Override
        public void finishUpdate(View arg0) {
        }
        @Override
        public int getCount() {
            return mListViews.size();
        }
        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            View image = mListViews.get(arg1);
            image.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					TurntableImageActivity.this.finish();
				}
			});
            return image;
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }
        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }
        @Override
        public Parcelable saveState() {
            return null;
        }
        @Override
        public void startUpdate(View arg0) {
        }
    }
		
}
